#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
#endregion

namespace pigs
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        MenuEntry PlayersMenuEntry;
        MenuEntry SoundMenuEntry;
        MenuEntry KeyboardMenuEntry;
        MenuEntry friendlyMenuEntry;
        MenuEntry rumbleMenuEntry;
        MenuEntry mouseMenuEntry;
        MenuEntry musicMenuEntry;

        string[] players = { "One", "Two", "Three", "Four", "Five" };

        GameSettings settings;

        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen(ScreenManager screen, GameSettings set, Boolean notPlaying)
            : base("Options")
        {
            settings = set;

            // Create our menu entries.
            PlayersMenuEntry = new MenuEntry(string.Empty);
            KeyboardMenuEntry = new MenuEntry(string.Empty);
            SoundMenuEntry = new MenuEntry(string.Empty);
            friendlyMenuEntry = new MenuEntry(string.Empty);
            rumbleMenuEntry = new MenuEntry(string.Empty);
            mouseMenuEntry = new MenuEntry(string.Empty);
            musicMenuEntry = new MenuEntry(string.Empty);
            MenuEntry blankMenuEntry = new MenuEntry(string.Empty);
            blankMenuEntry.isSelectable = false;

            SetMenuEntryText();

            MenuEntry back = new MenuEntry("Back");

            // Hook up menu event handlers.
            PlayersMenuEntry.Selected += PlayerMenuEntrySelected;
            KeyboardMenuEntry.Selected += KeyboardMenuEntrySelected;
            SoundMenuEntry.Selected += SoundMenuEntrySelected;
            friendlyMenuEntry.Selected += friendlyMenuEntrySelected;
            rumbleMenuEntry.Selected += rumbleMenuEntrySelected;
            mouseMenuEntry.Selected += mouseMenuEntrySelected;
            musicMenuEntry.Selected += musicMenuEntrySelected;
            back.Selected += OnCancel;
            
            // Add entries to the menu.
            if (notPlaying)
            {
                MenuEntries.Add(PlayersMenuEntry);
                MenuEntries.Add(KeyboardMenuEntry);
            }
            MenuEntries.Add(mouseMenuEntry);
            MenuEntries.Add(blankMenuEntry);

            MenuEntries.Add(SoundMenuEntry);
            MenuEntries.Add(musicMenuEntry);
            MenuEntries.Add(blankMenuEntry);

            MenuEntries.Add(friendlyMenuEntry);
            MenuEntries.Add(rumbleMenuEntry);
            MenuEntries.Add(back);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            PlayersMenuEntry.Text = "Players: " + players[settings.numberOfPlayers-1];
            KeyboardMenuEntry.Text = "Input: " + (settings.useKeyboard ? "Keyboard and gamepads" : "Only gamepads");
            SoundMenuEntry.Text = "Sound: " + (settings.playSound ? "On" : "Off");
            friendlyMenuEntry.Text = "Friendly Fire: " + (settings.friendlyFire ? "On" : "Off");
            rumbleMenuEntry.Text = "Force Feedback: " + (settings.useRumble ? "On" : "Off");
            mouseMenuEntry.Text = "Mouse Aiming: " + (settings.useMouse ? "On" : "Off");
            musicMenuEntry.Text = "Music: " + (settings.playMusic ? "On" : "Off");
        }

        /// <summary>
        /// Event handler for when the Language menu entry is selected.
        /// </summary>
        void PlayerMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            settings.numberOfPlayers = settings.numberOfPlayers + 1;
            
            //keep the value in range
            if (settings.useKeyboard && settings.numberOfPlayers == 6) settings.numberOfPlayers = 1;
            else if (!settings.useKeyboard && settings.numberOfPlayers == 5) settings.numberOfPlayers = 1;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void KeyboardMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            settings.useKeyboard = !settings.useKeyboard;
            if (settings.useKeyboard)
            {
                players = new string[]{ "One", "Two", "Three", "Four", "Five" };
            }
            else
            {
                players = new string[] { "One", "Two", "Three", "Four"};
                if (settings.numberOfPlayers - 1 == 4) settings.numberOfPlayers--;
            }


            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void SoundMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            settings.playSound = !settings.playSound;
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void friendlyMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            settings.friendlyFire = !settings.friendlyFire;
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void rumbleMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            settings.useRumble = !settings.useRumble;
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void mouseMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            settings.useMouse = !settings.useMouse;
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void musicMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            if (settings.playMusic)
            {
                settings.playMusic = false;
                settings.musicInstance.Pause();
            }
            else
            {
                settings.playMusic = true;
                settings.musicInstance.Play();
            }
            SetMenuEntryText();
        }

        #endregion
    }
}
